package cn.edu.scau.serviceImpl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import cn.edu.scau.Utils.SaveVideoUtils;
import cn.edu.scau.service.BaseService;

public class BaseServiceImpl<T> implements BaseService<T> {

		// 微博图片保存路径
		private String blogImagePath;
		// 视频初始保存路径
		private String baseVideoPath;
		// 截图
		private String screeshootPath;
		// 转码后视频保存路径
		private String flvVideoPath;
		// ffmeg的文件夹
		private String ffmpegPath;
		//头像的文件路径
		private String headImagePath;
		
		// 获取随机字符串
		protected String getRandomString(int length) {

			String base = "01234567890abcdefghijklmnopqrstuvwxyz" + "01234567890abcdefghijklmnopqrstuvwxyz";
			Random random = new Random();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < length; i++) {
				int number = random.nextInt(base.length());
				sb.append(base.charAt(number));
			}
			return sb.toString();
		}

		// 判断是否是图片
		public boolean isImage(String suffix) {
			// 转为大些字母
			suffix = suffix.toUpperCase();

			if (suffix.equals("JPEG"))
				return true;
			if (suffix.equals("JPG"))
				return true;
			if (suffix.equals("PNG"))
				return true;
			if (suffix.equals("BMP"))
				return true;
			if (suffix.equals("GIF"))
				return true;
			if (suffix.equals("EPS"))
				return true;
			if (suffix.equals("DCS"))
				return true;
			if (suffix.equals("PCX"))
				return true;
			if (suffix.equals("PDF"))
				return true;
			if (suffix.equals("RAW"))
				return true;
			if (suffix.equals("PICT"))
				return true;
			if (suffix.equals("PXR"))
				return true;
			if (suffix.equals("TIFF"))
				return true;
			if (suffix.equals("TARGA"))
				return true;
			
			return false;

		}

		public void add(T entity) {
			// TODO Auto-generated method stub
			
		}

		public boolean delete(Integer id) {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean update(T entity) {
			// TODO Auto-generated method stub
			return false;
		}

		public T find(Integer id) {
			// TODO Auto-generated method stub
			return null;
		}

		// 保存图片或者视频
		public List<Object> saveFiles(MultipartFile[] files, HttpServletRequest request) throws Throwable, IOException {

			// 获取各个文件夹的真是路径
			blogImagePath = request.getSession().getServletContext().getRealPath("/blogImage/");
			baseVideoPath = request.getSession().getServletContext().getRealPath("/baseVideo/");
			flvVideoPath = request.getSession().getServletContext().getRealPath("/flvVideo/");
			screeshootPath = request.getSession().getServletContext().getRealPath("/screeshot/");
			ffmpegPath = request.getSession().getServletContext().getRealPath("/ffmpeg/");
			headImagePath = request.getSession().getServletContext().getRealPath("/headImage/");
			// 用于保存图片路径
			List<String> imagesPath = null;
			// 用于保存视频路径
			String videoPath = null;
			//保存头像的路径
			String headPicturePath = null;
			//保存类型
			String type = request.getParameter("type");
			// 进行保存
			if(type.equals("1")){
				for (int i = 0; i < files.length; i++) {
					if (files[i].isEmpty())
						continue;
					// 获取文件的后缀
					String suffix = files[i].getOriginalFilename()
							.substring(files[i].getOriginalFilename().lastIndexOf(".") + 1);
					System.out.println(suffix);
					// 生成文件的名字
					String fileName = System.currentTimeMillis() + this.getRandomString(20) + "." + suffix;
					// 判断是图片还是视频
					if (isImage(suffix)) {
						System.out.println("是图片");
						// 为空则新建
						if (imagesPath == null) {
							imagesPath = new ArrayList<String>();
						}
						// 保存图片路径
						imagesPath.add("/blogImage/" + fileName);
						// 新建一个文件
						File targetFile = new File(blogImagePath + fileName);
						// 如果没有文件夹，就 创建文件夹
						if (!targetFile.exists()) {
							targetFile.mkdirs();
						}
						files[i].transferTo(targetFile);
					} else {
						System.out.println("是视频");
						videoPath = "/flvVideo/" + fileName;
						// 生成文件真是路径
						String srcFilePath = baseVideoPath + fileName;
						// 新建一个文件
						File targetFile = new File(srcFilePath);
						// 如果没有文件夹，就 创建文件夹
						if (!targetFile.exists()) {
							targetFile.mkdirs();
						}
						// 保存视频
						files[i].transferTo(targetFile);
						// 进行转码保存
						SaveVideoUtils.executeCodecs(ffmpegPath, baseVideoPath, srcFilePath, flvVideoPath + fileName + ".flv",
								screeshootPath + fileName + ".jpg");
					}
				}
				List<Object> path = new ArrayList<Object>();
				path.add(imagesPath);
				path.add(videoPath);
				return path;
			}else{
				// 获取文件的后缀
				String suffix = files[0].getOriginalFilename()
						.substring(files[0].getOriginalFilename().lastIndexOf(".") + 1);
				String fileName = System.currentTimeMillis() + this.getRandomString(20) + "." + suffix;
				//头像的相对路径
				headPicturePath = "/headImage/"+fileName;
				//文件对象
				File targetFile = new File(headImagePath + fileName);
				// 如果没有文件夹，就 创建文件夹
				if (!targetFile.exists()) {
					targetFile.mkdirs();
				}
				files[0].transferTo(targetFile);
				List<Object> path = new ArrayList<Object>();
				path.add(headPicturePath);
				return path;
			}	
		}
}
